package com.ruoyi.system.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {


    //声明一个普通的交换机
    @Bean("PublicExchange")
    public FanoutExchange publicExchange(){
        return new FanoutExchange("PublicExchange");
    }

    //声明一个普通的队列A
    @Bean("PublicQueueA")
    public Queue publicQueueA(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange", "letterExchange");
        map.put("x-dead-letter-routing-key", "deadKeyA");
        map.put("x-message-ttl", 10000);
        return QueueBuilder.durable("PublicQueueA").withArguments(map).build();
    }
    //声明一个普通的队列B
    @Bean("PublicQueueB")
    public Queue publicQueueB(){
       Map<String, Object> map = new HashMap<>();
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        map.put("x-dead-letter-exchange", "letterExchange");
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        map.put("x-dead-letter-routing-key", "deadKeyB");
        map.put("x-message-ttl", 10000);
        return QueueBuilder.durable("PublicQueueB").withArguments(map).build();
    }

    //声明一个死信交换机
    @Bean("letterExchange")
    public DirectExchange letterExchange(){
        return new DirectExchange("letterExchange");
    }

    //声明一个死信队列A
    @Bean("letterQueueA")
    public Queue letterQueueA(){
        return new Queue("letterQueueA");
    }
    //声明一个死信队列B
    @Bean("letterQueueB")
    public Queue letterQueueB(){
        return new Queue("letterQueueB");
    }


    //绑定普通交换机和普通队列A
    @Bean
    public Binding bindingPublicA(@Qualifier("PublicQueueA") Queue queue,@Qualifier("PublicExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    //绑定普通交换机和普通队列B
    @Bean
    public Binding bindingPublicB(@Qualifier("PublicQueueB") Queue queue,@Qualifier("PublicExchange")FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    //绑定死信交换机和死信队列A
    @Bean
    public Binding letterbingA(@Qualifier("letterQueueA")Queue queue,@Qualifier("letterExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("deadKeyA");
    }

    //绑定死信交换机和死信队列B
    @Bean
    public Binding letterbingB(@Qualifier("letterQueueB")Queue queue,@Qualifier("letterExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("deadKeyB");
    }


}
